<?php
namespace app\system\controller;

use app\common\model\ShopExpressModel;
use app\common\model\ShopOrderGoodsModel;
use app\common\model\ShopOrderLogModel;
use app\common\model\ShopOrderModel;
use app\common\model\UserModel;
use app\common\model\ShopOrderRefundModel;
use think\Request;

class ShopOrderRefundController extends BaseController
{

    private $orderModel;
    private $orderLogModel;
    private $orderGoodsModel;
    private $orderRefundModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->orderModel       = new ShopOrderModel();
        $this->orderGoodsModel  = new ShopOrderGoodsModel();
        $this->orderLogModel    = new ShopOrderLogModel();
        $this->orderRefundModel = new ShopOrderRefundModel();
    }

    /**
     * 首页
     */
    public function index()
    {
        $map               = [];
        $query             = [];
        $map['site_id'] = $this->getSiteId();
        $query['site_id'] = $this->getSiteId();
        $orderSn           = $this->request->param('order_sn');
        $query['order_sn'] = $orderSn;
        if (!empty($orderSn)) {
            $map['order_sn'] = ['like', '%' . $orderSn . '%'];
        }
        $orderGenre           = $this->request->param('order_genre', 0);
        $query['order_genre'] = $orderGenre;
        if ($orderGenre == 1) { //待审核
            $map['seller_status'] = 0;
        }
        if ($orderGenre == 2) { //不同意
            $map['seller_status'] = -1;
        }
        if ($orderGenre == 3) { //已完成
            $map['seller_status'] = 1;
        }
        $map['site_id'] = $this->getSiteId();
        $list           = $this->orderRefundModel->where($map)->order('create_time desc')->paginate(12, false, [
            'query' => $query,
        ]);
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data       = $this->request->param();
            $refundInfo = $this->orderRefundModel->find($data['refund_id']);
            $orderInfo  = $this->orderModel->find($refundInfo['order_id']);
            if ($data['seller_status'] == 1) { //同意
                if ($data['refund_amount'] > ($orderInfo['order_price'] - $orderInfo['refund_amount'])) {
                    $this->error('对不起，你最多还可以退' . ($orderInfo['order_price'] - $orderInfo['refund_amount']) . '元');
                }
                //退回会员帐户
                $refundType = false;
                if ($refundInfo['refund_type'] == 1) { //退回会员帐户
                    $refundType = true;
                }
                if ($refundInfo['refund_type'] == 3 && $orderInfo['pay_code'] == 'userpay') { //原路退回
                    $refundType = true;
                }
                if ($refundType == true) {
                    $userMemberAccountModel = new \app\common\model\UserMemberAccountModel();
                    $accountData            = [
                        'site_id'    => $orderInfo['site_id'],
                        'system_uid' => $this->getUid(),
                        'member_uid' => $orderInfo['buyer_uid'],
                        'event'      => 4,
                        'amount'     => $data['refund_amount'],
                        'order_sn'   => $orderInfo['order_sn'],
                    ];
                    $result                 = $userMemberAccountModel->changeAccount($accountData);
                    if ($result === false) {
                        $this->error($userMemberAccountModel->getError());
                    }
                }
                //更新订单表
                $order['order_id']      = $orderInfo['order_id'];
                $order['refund_amount'] = $orderInfo['refund_amount'] + $data['refund_amount'];
                if ($order['refund_amount'] < $orderInfo['order_price'] - $orderInfo['refund_amount']) {
                    $order['refund_status'] = 1; //部分退
                }
                if ($order['refund_amount'] == $orderInfo['order_price'] - $orderInfo['refund_amount']) {
                    $order['refund_status'] = 2; //全部退
                }
                $order['refund_time'] = time();
                $this->orderModel->editData($order);
                //更新退款单
                $data['refund_status'] = 2; //已完成
                $data['refund_time'] = time(); //退款时间
                $this->orderRefundModel->editData($data);
                //写入订单日志
                $orderLogModel = new ShopOrderLogModel();
                $orderLogModel->addOrderLog($data['action_name'], $orderInfo['order_id'], $orderInfo['order_status'], $orderInfo['shipping_status'], $orderInfo['pay_status'], $data['action_name'] . '受理了订单退款');
                $this->success('操作成功！','index');
            }
            if ($data['seller_status'] == -1) {
                $data['refund_status'] = 2;
                $this->orderRefundModel->editData($data);
                //写入日志
                $orderLogModel = new ShopOrderLogModel();
                $orderLogModel->addOrderLog($data['action_name'], $orderInfo['order_id'], $orderInfo['order_status'], $orderInfo['shipping_status'], $orderInfo['pay_status'], $data['action_name'] . '拒绝了订单退款');
                $this->success('操作成功！','index');
            }
        }
        $userModel  = new UserModel();
        $actionName = $userModel->where('uid', $this->getUid())->value('username');
        $refundId   = $this->request->param('refund_id');
        $refundInfo = $this->orderRefundModel->find($refundId);
        $orderInfo  = $this->orderModel->get($refundInfo['order_id']);
        $this->assign('actionName', $actionName);
        $this->assign('orderInfo', $orderInfo);
        $this->assign('refundInfo', $refundInfo);
        return $this->fetch();
    }

    public function del()
    {
        $refundId = $this->request->param('refund_id/d');
        $this->orderRefundModel->delData($refundId);
        $this->success('删除成功！', url('index'));
    }
}
